package cn.iocoder.yudao.module.eval.controller.admin.monitoring;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.monitoring.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.monitoring.EvalMonitoringDataDO;
import cn.iocoder.yudao.module.eval.service.monitoring.EvalMonitoringDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.framework.common.pojo.PageParam;

@Tag(name = "管理后台 - 评价实施监控")
@RestController
@RequestMapping("/eval/monitoring-data")
@Validated
public class EvalMonitoringDataController {

    @Resource
    private EvalMonitoringDataService monitoringDataService;

    @PostMapping("/create")
    @Operation(summary = "创建评价实施监控")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:create')")
    public CommonResult<Long> createMonitoringData(@Valid @RequestBody EvalMonitoringDataSaveReqVO createReqVO) {
        return success(monitoringDataService.createMonitoringData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评价实施监控")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:update')")
    public CommonResult<Boolean> updateMonitoringData(@Valid @RequestBody EvalMonitoringDataSaveReqVO updateReqVO) {
        monitoringDataService.updateMonitoringData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评价实施监控")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:monitoring:delete')")
    public CommonResult<Boolean> deleteMonitoringData(@RequestParam("id") Long id) {
        monitoringDataService.deleteMonitoringData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评价实施监控")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:query')")
    public CommonResult<EvalMonitoringDataRespVO> getMonitoringData(@RequestParam("id") Long id) {
        EvalMonitoringDataDO monitoringData = monitoringDataService.getMonitoringData(id);
        return success(BeanUtils.toBean(monitoringData, EvalMonitoringDataRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得评价实施监控列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:query')")
    public CommonResult<List<EvalMonitoringDataRespVO>> getMonitoringDataList(@RequestParam("ids") Collection<Long> ids) {
        List<EvalMonitoringDataDO> list = monitoringDataService.getMonitoringDataList(ids);
        return success(BeanUtils.toBean(list, EvalMonitoringDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评价实施监控分页")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:query')")
    public CommonResult<PageResult<EvalMonitoringDataRespVO>> getMonitoringDataPage(@Valid EvalMonitoringDataPageReqVO pageReqVO) {
        PageResult<EvalMonitoringDataDO> pageResult = monitoringDataService.getMonitoringDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalMonitoringDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评价实施监控 Excel")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonitoringDataExcel(@Valid EvalMonitoringDataPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalMonitoringDataDO> list = monitoringDataService.getMonitoringDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评价实施监控.xls", "数据", EvalMonitoringDataRespVO.class,
                BeanUtils.toBean(list, EvalMonitoringDataRespVO.class));
    }

    @GetMapping("/list-by-plan")
    @Operation(summary = "根据计划ID获得监控数据列表")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:query')")
    public CommonResult<List<EvalMonitoringDataRespVO>> getMonitoringDataListByPlanId(@RequestParam("planId") Long planId) {
        List<EvalMonitoringDataDO> list = monitoringDataService.getMonitoringDataListByPlanId(planId);
        return success(BeanUtils.toBean(list, EvalMonitoringDataRespVO.class));
    }

    @PutMapping("/handle-exception")
    @Operation(summary = "处理监控异常")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:handle')")
    public CommonResult<Boolean> handleMonitoringException(@Valid @RequestBody EvalMonitoringDataHandleReqVO handleReqVO) {
        monitoringDataService.handleMonitoringException(handleReqVO.getId(), handleReqVO.getHandleResult());
        return success(true);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取监控统计数据")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:monitoring:query')")
    public CommonResult<Object> getMonitoringStatistics(@RequestParam("planId") Long planId) {
        return success(monitoringDataService.getMonitoringStatistics(planId));
    }

}
